home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / UTILITIE / OKUMURA.ZIP / COMPRESS.TXT next >
Text File  |  1989-04-09  |  13KB  |  278 lines

  1.          Data Compression Algorithms of LARC and LHarc
  2.                       Haruhiko Okumura*
  3. *The author is the sysop of the Science SIG of PV-VAN.  His
  4.  address is: 12-2-404 Green Heights, 580 Nagasawa, Yokosuka 239
  5.  Japan
  6. ---------------------------------------------------------------
  7.  
  8. 1. Introduction
  9.  
  10. In the spring of 1988, I wrote a very simple data compression program
  11. named LZSS in C language, and uploaded it to the Science SIG (forum) of
  12. PC-VAN, Japan's biggest personal computer network.
  13.  
  14. That program was based on Storer and Szymanski's slightly modified
  15. version of one of Lempel and Ziv's algorithms.  Despite its simplicity,
  16. for most files its compression outperformed the archivers then widely
  17. used.
  18.  
  19. Kazuhiko Miki rewrote my LZSS in Turbo Pascal and assembly language, and
  20. soon made it evolve into a complete archiver, which he named LARC.
  21.  
  22. The first versions of LZSS and LARC were rather slow.  So I rewrote my
  23. LZSS using a binary tree, and so did Miki.  Although LARC's encoding was
  24. slower than the fastest archiver available, its decoding was quite fast,
  25. and its algorithm was so simple that even self-extracting files
  26. (compressed files plus decoder) it created were usually smaller than
  27. non-self-extracting files from other archivers.
  28.  
  29. Soon many hobby programmers joined the archiver project at the forum.
  30. Very many suggestions were made, and LARC was revised again and again.
  31. By the summer of 1988, LARC's speed and compression have improved so
  32. much that LARC-compressed programs were beginning to be uploaded in many
  33. forums of PC-VAN and other networks.
  34.  
  35. In that summer I wrote another program, LZARI, which combined the LZSS
  36. algorithm with adaptive arithmetic compression.  Although it was slower
  37. than LZSS, its compression performance was amazing.
  38.  
  39. Miki, the author of LARC, uploaded LZARI to NIFTY-Serve, another big
  40. information network in Japan.  In NIFTY-Serve, Haruyasu Yoshizaki
  41. replaced LZARI's adaptive arithmetic coding with a version of adaptive
  42. Huffman coding to increase speed.  Based on this algorithm, which he
  43. called LZHUF, he developed yet another archiver, LHarc.
  44.  
  45. In what follows, I will review several of these algorithms and supply
  46. simplified codes in C language.
  47.  
  48. 2. Simple coding methods
  49.  
  50. Replacing several (usually 8 or 4) "space" characters by one "tab"
  51. character is a very primitive method for data compression.  Another
  52. simple method is run-length coding, which encodes the message
  53. "AAABBBBAACCCC" into "3A4B2A4C", for example.
  54.  
  55. 3. LZSS coding
  56.  
  57. This scheme is initiated by Ziv and Lempel [1].  A slightly modified
  58. version is described by Storer and Szymanski [2].  An implementation
  59. using a binary tree is proposed by Bell [3].  The algorithm is quite
  60. simple: Keep a ring buffer, which initially contains "space" characters
  61. only.  Read several letters from the file to the buffer.  Then search
  62. the buffer for the longest string that matches the letters just read,
  63. and send its length and position in the buffer.
  64.  
  65. If the buffer size is 4096 bytes, the position can be encoded in 12
  66. bits.  If we represent the match length in four bits, the <position,
  67. length> pair is two bytes long.  If the longest match is no more than
  68. two characters, then we send just one character without encoding, and
  69. restart the process with the next letter.  We must send one extra bit
  70. each time to tell the decoder whether we are sending a <position,
  71. length> pair or an unencoded character.
  72.  
  73. The accompanying file LZSS.C is a version of this algorithm.  This
  74. implementation uses multiple binary trees to speed up the search for the
  75. longest match.  All the programs in this article are written in
  76. draft-proposed ANSI C.  I tested them with Turbo C 2.0.
  77.  
  78. 4. LZW coding
  79.  
  80. This scheme was devised by Ziv and Lempel [4], and modified by Welch
  81. [5].
  82.  
  83. The LZW coding has been adopted by most of the existing archivers, such
  84. as ARC and PKZIP.  The algorithm can be made relatively fast, and is
  85. suitable for hardware implementation as well.
  86.  
  87. The algorithm can be outlined as follows: Prepare a table that can
  88. contain several thousand items.  Initially register in its 0th through
  89. 255th positions the usual 256 characters.  Read several letters from the
  90. file to be encoded, and search the table for the longest match.  Suppose
  91. the longest match is given by the string "ABC".  Send the position of
  92. "ABC" in the table.  Read the next character from the file.  If it is
  93. "D", then register a new string "ABCD" in the table, and restart the
  94. process with the letter "D".  If the table becomes full, discard the
  95. oldest item or, preferably, the least used.
  96.  
  97. A Pascal program for this algorithm is given in Storer's book [6].
  98.  
  99. 5. Huffman coding
  100.  
  101. Classical Huffman coding is invented by Huffman [7].  A fairly readable
  102. accound is given in Sedgewick [8].
  103.  
  104. Suppose the text to be encoded is "ABABACA", with four A's, two B's, and
  105. a C.  We represent this situation as follows:
  106.  
  107.         4    2    1
  108.         |    |    |
  109.         A    B    C
  110.  
  111. Combine the least frequent two characters into one, resulting in the new
  112. frequency 2 + 1 = 3:
  113.  
  114.         4      3
  115.         |     /  \
  116.         A    B    C
  117.  
  118. Repeat the above step until the whole characters combine into a tree:
  119.  
  120.             7
  121.           /  \
  122.          /     3
  123.         /    /  \
  124.        A    B    C
  125.  
  126. Start at the top ("root") of this encoding tree, and travel to the
  127. character you want to encode.  If you go left, send a "0"; otherwise
  128. send a "1".  Thus, "A" is encoded by "0", "B" by "10", "C" by "11".
  129. Algotether, "ABABACA" will be encoded into ten bits, "0100100110".
  130.  
  131. To decode this code, the decoder must know the encoding tree, which must
  132. be sent separately.
  133.  
  134. A modification to this classical Huffman coding is the adaptive, or
  135. dynamic, Huffman coding.  See, e.g., Gallager [9].  In this method, the
  136. encoder and the decoder processes the first letter of the text as if the
  137. frequency of each character in the file were one, say.  After the first
  138. letter has been processed, both parties increment the frequency of that
  139. character by one.  For example, if the first letter is 'C', then
  140. freq['C'] becomes two, whereas every other frequencies are still one.
  141. Then the both parties modify the encoding tree accordingly.  Then the
  142. second letter will be encoded and decoded, and so on.
  143.  
  144. 6. Arithmetic coding
  145.  
  146. The original concept of arithmetic coding is proposed by P.  Elias.  An
  147. implementation in C language is described by Witten and others [10].
  148.  
  149. Although the Huffman coding is optimal if each character must be encoded
  150. into a fixed (integer) number of bits, arithmetic coding wins if no such
  151. restriction is made.
  152.  
  153. As an example we shall encode "AABA" using arithmetic coding.  For
  154. simplicity suppose we know beforehand that the probabilities for "A" and
  155. "B" to appear in the text are 3/4 and 1/4, respectively.
  156.  
  157. Initially, consider an interval:
  158.  
  159.               0 <= x < 1.
  160.  
  161. Since the first character is "A" whose probability is 3/4, we shrink the
  162. interval to the lower 3/4:
  163.  
  164.               0 <= x < 3/4.
  165.  
  166. The next character is "A" again, so we take the lower 3/4:
  167.  
  168.               0 <= x < 9/16.
  169.  
  170. Next comes "B" whose probability is 1/4, so we take the upper 1/4:
  171.  
  172.               27/64 <= x < 9/16,
  173.  
  174. because "B" is the second element in our alphabet, {A, B}.  The last
  175. character is "A" and the interval is
  176.  
  177.               27/64 <= x < 135/256,
  178.  
  179. which can be written in binary notation
  180.  
  181.               0.011011 <= x < 0.10000111.
  182.  
  183. Choose from this interval any number that can be represented in fewest
  184. bits, say 0.1, and send the bits to the right of "0."; in this case we
  185. send only one bit, "1".  Thus we have encoded four letters into one bit!
  186. With the Huffman coding, four letters could not be encoded into less
  187. than four bits.
  188.  
  189. To decode the code "1", we just reverse the process: First, we supply
  190. the "0." to the right of the received code "1", resulting in "0.1" in
  191. binary notation, or 1/2.  Since this number is in the first 3/4 of the
  192. initial interval 0 <= x < 1, the first character must be "A".  Shrink
  193. the interval into the lower 3/4.  In this new interval, the number 1/2
  194. lies in the lower 3/4 part, so the second character is again "A", and so
  195. on.  The number of letters in the original file must be sent separately
  196. (or a special 'EOF' character must be appended at the end of the file).
  197.  
  198. The algorithm described above requires that both the sender and receiver
  199. know the probability distribution for the characters.  The adaptive
  200. version of the algorithm removes this restriction by first supposing
  201. uniform or any agreed-upon distribution of characters that approximates
  202. the true distribution, and then updating the distribution after each
  203. character is sent and received.
  204.  
  205. 7. LZARI
  206.  
  207. In each step the LZSS algorithm sends either a character or a <position,
  208. length> pair.  Among these, perhaps character "e" appears more
  209. frequently than "x", and a <position, length> pair of length 3 might be
  210. commoner than one of length 18, say.  Thus, if we encode the more
  211. frequent in fewer bits and the less frequent in more bits, the total
  212. length of the encoded text will be diminished.  This consideration
  213. suggests that we use Huffman or arithmetic coding, preferably of
  214. adaptive kind, along with LZSS.
  215.  
  216. This is easier said than done, because there are many possible
  217. <position, length> combinations.  Adaptive compression must keep running
  218. statistics of frequency distribution.  Too many items make statistics
  219. unreliable.
  220.  
  221. What follows is not even an approximate solution to the problem posed
  222. above, but anyway this was what I did in the summer of 1988.
  223.  
  224. I extended the character set from 256 to three-hundred or so in size,
  225. and let characters 0 through 255 be the usual 8-bit characters, whereas
  226. characters 253 + n represent that what follows is a position of string
  227. of length n, where n = 3, 4 , ....  These extended set of characters
  228. will be encoded with adaptive arithmetic compression.
  229.  
  230. I also observed that longest-match strings tend to be the ones that were
  231. read relatively recently.  Therefore, recent positions should be encoded
  232. into fewer bits.  Since 4096 positions are too many to encode
  233. adaptively, I fixed the probability distribution of the positions "by
  234. hand." The distribution function given in the accompanying LZARI.C is
  235. rather tentative; it is not based on thorough experimentation.  In
  236. retrospect, I could encode adaptively the most significant 6 bits, say,
  237. or perhaps by some more ingenious method adapt the parameters of the
  238. distribution function to the running statistics.
  239.  
  240. At any rate, the present version of LZARI treats the positions rather
  241. separately, so that the overall compression is by no means optimal.
  242. Furthermore, the string length threshold above which strings are coded
  243. into <position, length> pairs is fixed, but logically its value must
  244. change according to the length of the <position, length> pair we would
  245. get.
  246.  
  247. 8. LZHUF
  248.  
  249. LZHUF, the algorithm of Haruyasu Yoshizaki's archiver LHarc, replaces
  250. LZARI's adaptive arithmetic coding with adaptive Huffman.  LZHUF encodes
  251. the most significant 6 bits of the position in its 4096-byte buffer by
  252. table lookup.  More recent, and hence more probable, positions are coded
  253. in less bits.  On the other hand, the remaining 6 bits are sent
  254. verbatim.  Because Huffman coding encodes each letter into a fixed
  255. number of bits, table lookup can be easily implemented.
  256.  
  257. Though theoretically Huffman cannot exceed arithmetic compression, the
  258. difference is very slight, and LZHUF is fairly fast.
  259.  
  260. The accompanying file LZHUF.C was written by Yoshizaki.  I translated
  261. the comments into English and made a few trivial changes to make it
  262. conform to the ANSI C standard.
  263.  
  264. References
  265.   [1] J. Ziv and A. Lempel, IEEE Trans. IT-23, 337-343 (1977).
  266.   [2] J. A. Storer and T. G. Szymanski, J. ACM, 29, 928-951
  267.       (1982).
  268.   [3] T. C. Bell, IEEE Trans. COM-34, 1176-1182 (1986).
  269.   [4] J. Ziv and A. Lempel, IEEE Trans. IT-24, 530-536 (1978).
  270.   [5] T. A. Welch, Computer, 17, No.6, 8-19 (1984).
  271.   [6] J. A. Storer, Data Compression: Methods and Theory
  272.       (Computer Science Press, 1988).
  273.   [7] D. A. Huffman, Proc IRE 40, 1098-1101 (1952).
  274.   [8] R. Sedgewick, Algorithms, 2nd ed. (Addison-Wesley, 1988).
  275.   [9] R. G. Gallager, IEEE Trans. IT-24, 668-674 (1978).
  276.  [10] I. E. Witten, R. M. Neal, and J. G. Cleary, Commun. ACM
  277.       30, 520-540 (1987).
  278.